package com.atelier.plugin.database.ui;

import com.atelier.plugin.database.action.MyCollapseAllAnAction;
import com.atelier.plugin.database.action.MyExpandAllAction;
import com.atelier.plugin.projectWizard.util.ICONS;
import com.intellij.icons.AllIcons;
import com.intellij.ide.CommonActionsManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowEP;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.openapi.wm.impl.SquareStripeButton;
import com.intellij.openapi.wm.impl.ToolwindowToolbar;
import com.intellij.ui.GuiUtils;
import com.intellij.ui.content.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author tangrufei
 * @date 2023-09-06 10:29
 */
public class DataBaseToolWindow  implements ToolWindowFactory, DumbAware
{
    public static Logger LOGGER=  LoggerFactory.getLogger(DataBaseToolWindow.class);



    /**
     * @param project
     * @param toolWindow
     */
    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow)
    {
        // 此处方法将会在点击ToolWindow的时候触发
        // 获取ContentManager
        ContentManager contentManager = toolWindow.getContentManager();
        DataBaseToolWindowContent dataBaseToolWindowContent;
        try {
            dataBaseToolWindowContent = DataBaseToolWindowContent.getInstants();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        Content content =
                contentManager.getFactory()
                        // 内容管理器获取工厂类
                        .createContent( // 创建Content（组件类实例、显示名称、是否可以锁定）
                                dataBaseToolWindowContent.getContent(),
                                "",
                                true
                        );


        toolWindow.setIcon(ICONS.DataBase);
        contentManager.addContent(content);

        CommonActionsManager actionsManager = CommonActionsManager.getInstance();
        toolWindow.setTitleActions(Arrays.asList(actionsManager.createExpandAllHeaderAction(dataBaseToolWindowContent.getDatabaseTree()), actionsManager.createCollapseAllHeaderAction(dataBaseToolWindowContent.getDatabaseTree())));

    }

    @NotNull
    private static ArrayList<AnAction> getAnActions() {
        ArrayList<AnAction> anActions = new ArrayList<>();
        MyExpandAllAction myExpandAllAction = new MyExpandAllAction();
        MyCollapseAllAnAction myCollapseAllAnAction = new MyCollapseAllAnAction();
        anActions.add(myExpandAllAction);
        anActions.add(myCollapseAllAnAction);
        return anActions;
    }

    /**
     * Perform additional initialization routine here.
     *
     * @param toolWindow
     */
    @Override
    public void init(@NotNull ToolWindow toolWindow)
    {
        ToolWindowFactory.super.init(toolWindow);

    }

    /**
     * Check if tool window (and its stripe button) should be visible after startup.
     *
     * @param project
     * @see ToolWindow#isAvailable()
     */
    @Override
    public boolean shouldBeAvailable(@NotNull Project project)
    {
        LOGGER.info("shouldBeAvailable");
        return ToolWindowFactory.super.shouldBeAvailable(project);
    }


    @Override
    @Deprecated
    public boolean isDoNotActivateOnStart()
    {
        LOGGER.info("isDoNotActivateOnStart");
        return ToolWindowFactory.super.isDoNotActivateOnStart();
    }

}
